package com.zw.weiyi.fragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Toast;

import com.zw.weiyi.adapter.SMSAdpter;
import com.zw.weiyi.avtivity.CallInfo;
import com.zw.weiyi.avtivity.InfoActivity;
import com.zw.weiyi.avtivity.MessageBoxList;
import com.zw.weiyi.enety.Call;
import com.zw.weiyi.enety.Person;
import com.zw.weiyi.enety.SMS;
import com.zw.weiyi.phonesms.R;
import com.zw.weiyi.util.BaseIntentUtil;
import com.zw.weiyi.util.CallUtil;
import com.zw.weiyi.util.FiledDraw;
import com.zw.weiyi.util.MessageUtil;
import com.zw.weiyi.util.PhoneUtil;
import com.zw.weiyi.util.SMSUtil;
/**
 * 
 * @author Striver_zw
 *   Date  2014-12-13
 */
@SuppressLint("ValidFragment")
public class SmsFragment extends Fragment implements OnItemLongClickListener{
	private View view;//缓存页面

	private ListView smsListView;
	private SMSAdpter smsAdpter;
	private SMSUtil msms;
	
	private Context context;

	private List<SMS> list_mmt;
	
	@SuppressLint("ValidFragment")
	public SmsFragment(Context context) {
		this.context = context;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if(view==null){
			view=inflater.inflate(R.layout.sms_fragment,container, false);
		}
		ViewGroup parent = (ViewGroup) view.getParent();
		if(parent!=null){
			parent.removeView(view);//先移除
		}
		// 初始化
		initview(view);
		return view;
	}
	
	//初始化
	public void initview(View view) {
		smsListView = (ListView) view.findViewById(R.id.sms_list);
		smsListView.setOnItemLongClickListener(this);
		smsAdpter = new SMSAdpter(context);
		msms = new SMSUtil(context);
		list_mmt = msms.getThreadsNum(msms.getThreads(0));
		smsAdpter.assignment(list_mmt);
		smsListView.setAdapter(smsAdpter);
		
		FiledDraw.setDraw(context, smsListView);
		
		smsListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				SMS sb = (SMS) smsAdpter.getItem(position);				
				Intent intent = new Intent(context, MessageBoxList.class);
				// 通过意图发送数据
				if(sb.getName()==null){
					intent.putExtra("name", sb.getAddress());
				}else{
					intent.putExtra("name", sb.getName());
				}
				intent.putExtra("phoneNumber", sb.getAddress());				
				intent.putExtra("threadId", sb.getThread_id());								
				context.startActivity(intent);
			}
		});
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> adapterView, View view,
			final int position, long id) {
		final SMS sms = list_mmt.get(position);
		new AlertDialog.Builder(context)
		.setTitle("操作")
		.setItems(new String[]{"拨号","删除该联系人的所有信息"},
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog,
							int which) {
						if (which == 0) {//
							PhoneUtil.phoneNumber(context, sms.getAddress());									
						} else if (which == 1) {// 删除
							MessageUtil.deleteMessage(context, sms);
							Toast.makeText(context, "删除成功", 1000).show();
							list_mmt.remove(position);
							smsAdpter.notifyDataSetChanged();
						}
					}
				}).show();
		return false;
	}
}
